package com.dk.controller;

import com.dk.data.Result;
import com.dk.em.ResultEnum;
import com.dk.service.ITUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("tuser")
@Api("用户的登录")
public class TUserController {

    @Autowired
    private ITUserService userServicer;

    @PostMapping("login")
    @ResponseBody
    @ApiOperation("用户的登录")
    public Result login(String tname , String tpwd){
        //获取subject
        Subject subject = SecurityUtils.getSubject();
        //封装token
        UsernamePasswordToken token = new UsernamePasswordToken(tname , tpwd);
        //校验
        try {
            subject.login(token);
        }catch (UnknownAccountException e){
            e.printStackTrace();
            return Result.response(ResultEnum.NOLOGIN).setData(e);
        }catch (IncorrectCredentialsException e){
            e.printStackTrace();
            return Result.response(ResultEnum.GATEWAY_FALLBACK);
        }
        return Result.ok();
    }
}
